import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    DeviceRegisterState state, Dispatch dispatch, ViewService viewService) {
  var isUnitDevice = state.unit != null;
  var location = isUnitDevice
      ? '${state.build.buildingName}-${state.unit.itemName}'
      : '围墙机';
  return Scaffold(
    appBar: AppBar(
      title: Text('注册门禁'),
      actions: <Widget>[
        IconButton(
          icon: Icon(Icons.send),
          onPressed: () {
            dispatch(DeviceRegisterActionCreator.register());
          },
        )
      ],
    ),
    body: GestureDetector(
      onTap: () {
        FocusScope.of(viewService.context).requestFocus(new FocusNode());
      },
      child: ListView(
        children: <Widget>[
          Text(
            '门禁位置:${location}',
          ),
          Row(
            children: <Widget>[
              Text(
                '门禁分类:',
              ),
              Radio(
                groupValue: false,
                value: isUnitDevice,
              ),
              Text('围墙机'),
              Radio(
                groupValue: true,
                value: isUnitDevice,
              ),
              Text('单元机'),
            ],
          ),
          Text(
            '此处类型不可修改，根据上一个界面的选择自动获取，需要修改请返回上一个界面',
            style: TextStyle(color: Colors.red[200], fontSize: 10),
          ),
          Row(
            children: <Widget>[
              Text(
                '门禁名称:',
              ),
              Expanded(
                child: TextField(
                  controller: state.nameController,
                  onChanged: (msg) {},
                  decoration: InputDecoration(
                    hintText: '请输入门禁名称',
                    labelText: '门禁名称',
                  ),
                  maxLines: 1,
                  maxLength: 10,
                  style: TextStyle(fontSize: 12),
                  // keyboardType: TextInputType.multiline,
                ),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Text(
                '设备编号:',
              ),
              Expanded(
                child: TextField(
                  controller: state.deviceNoController,
                  onChanged: (msg) {},
                  decoration: InputDecoration(
                    hintText: '设备编号，通常是mac',
                    labelText: '设备编号',
                  ),
                  maxLines: 1,
                  maxLength: 20,
                  style: TextStyle(fontSize: 12),
                  // keyboardType: TextInputType.multiline,
                ),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Text(
                '设备厂商:',
              ),
              Expanded(
                child: DropdownButtonFormField(
                  value: state.selectedVendorId,
                  items: _createVenorList(state, dispatch, viewService),
                  onChanged: (item) {
                    dispatch(
                        DeviceRegisterActionCreator.changeDeviceVendor(item));
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    ),
  );
}

List<DropdownMenuItem> _createVenorList(
    DeviceRegisterState state, Dispatch dispatch, ViewService viewService) {
  var list = List<DropdownMenuItem>();
  state.vendorList.forEach((vendorInfo) {
    var item = DropdownMenuItem(
      value: vendorInfo.deviceTypeId,
      child: Text(vendorInfo.deviceTypeName),
    );
    list.add(item);
  });

  return list;
}
